package com.huluobo.a2112mvvmdouyindemo.startLive;

import static com.tencent.live2.V2TXLiveCode.V2TXLIVE_ERROR_INVALID_LICENSE;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.huluobo.a2112mvvmdouyindemo.R;
import com.huluobo.a2112mvvmdouyindemo.databinding.ActivityStartLiveBinding;
import com.huluobo.mvvm_base.view.BaseActivity;
import com.tencent.live2.V2TXLiveDef;
import com.tencent.live2.V2TXLivePlayer;
import com.tencent.live2.V2TXLivePusher;
import com.tencent.live2.impl.V2TXLivePlayerImpl;
import com.tencent.live2.impl.V2TXLivePusherImpl;
import com.tencent.rtmp.ui.TXCloudVideoView;

public class StartLiveActivity extends BaseActivity<ActivityStartLiveBinding> {

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        // 指定对应的直播协议为 RTMP，该协议不支持连麦
        V2TXLivePusher mLivePusher = new V2TXLivePusherImpl(this, V2TXLiveDef.V2TXLiveMode.TXLiveMode_RTMP);

        // 启动本地摄像头预览
        mLivePusher.setRenderView(dataBinding.pusherTxCloudView);
        mLivePusher.startCamera(true);
        mLivePusher.startMicrophone();

        dataBinding.startLiveStartPush.setOnClickListener(v -> {
            // 根据推流协议传入对应的 URL 即可启动推流， RTMP 协议以 rtmp:// 开头，该协议不支持连麦
            String url = "rtmp://198116.push.tlivecloud.com/live/123?txSecret=d2d01205d41cf2db946b89c2332c27a5&txTime" +
                    "=6614CEE7";
            int ret = mLivePusher.startPush(url);
            if (ret == V2TXLIVE_ERROR_INVALID_LICENSE) {
                Log.i("TagA", "startRTMPPush: license 校验失败");
            }
        });

        dataBinding.startLiveStopPush.setOnClickListener(v -> {
            // 结束推流
            mLivePusher.stopPush();
        });

//创建 player 对象
        V2TXLivePlayer mLivePlayer = new V2TXLivePlayerImpl(this);
//关键 player 对象与界面 view
        mLivePlayer.setRenderView(dataBinding.videoView);

        dataBinding.startLivePlay.setOnClickListener(v -> {
            String url = "rtmp://198116.push.tlivecloud.com/play/123?txSecret=d2d01205d41cf2db946b89c2332c27a5&txTime" +
                    "=6614CEE7";
            //http://123.liveplay.myqcloud.com/live/123.flv
            mLivePlayer.startLivePlay("http://123.liveplay.myqcloud.com/live/123.flv");
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_start_live;
    }
}